<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/8/3 18:22
 */

namespace App\Project\Traits;

use App\Models\Reservation\Ability;
use App\Models\Reservation\Role;

trait DoctorAndHospitalLinkage
{
    protected function linkageWithAnAbility($ability)
    {
        $user = \Auth::user();

        $hospitals = null;
        $doctors   = null;

        if ($user->isAn('admin')) {
            $ability = Ability::where('name', $ability)->first();

            $hospitals = $ability->hospitals()->hasDoctors()->get(['id', 'name']);

            $doctors = $hospitals->first()->doctors(['id', 'name'])->getResults();
        } else {
            $hospitals = $user->hospital;

            if ($user->isA('nurse')) {
                $doctors = Role::where('name', 'doctor')->doctors($hospitals->id)->first()->users;
            } else {
                $doctors = $user;
            }
        }

        return compact('doctors', 'hospitals');
    }
}